//
//  ZRVisitorView.swift
//  Sina
//
//  Created by 张锐 on 16/2/25.
//  Copyright © 2016年 zhangr. All rights reserved.
//

import UIKit
import SnapKit

protocol ZRVisitorViewDelegate : NSObjectProtocol{

    func login()
    
    func register()


}

class ZRVisitorView: UIView {
    
    /**代理对象
     */
    weak var delegate : ZRVisitorViewDelegate?
    
    /**
     接口设置界面样式
     
     - parameter centerImageName: 中间图片名称
     - parameter text:            文字
     - parameter animation:       是否动画
     */
    func settingUI(centerImageName : String , text : String , animation : Bool){
        
        if animation {
        
            startRotationAnimation()
            
        }else{
        
            rotationImageView.alpha = 0
        }
        
        centerImageView.image = UIImage(named: centerImageName)
        
        textLabel.text = text
    }

    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //创建一个私有方法来添加控件和设置约束
    private func setUpUI(){
        
        //添加控件
        addSubview(rotationImageView)
        
        addSubview(coverImageView)
        
        addSubview(centerImageView)
        
        addSubview(textLabel)
        
        addSubview(registerButton)
        
        addSubview(loginButton)
        
        //添加约束
        rotationImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self.snp_center)
        }
        
        centerImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self.snp_center)
        }
        textLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(rotationImageView.snp_bottom).offset(10)
            
            make.centerX.equalTo(self.snp_centerX)
            
            make.width.equalTo(216)
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(textLabel.snp_left)
            
            make.top.equalTo(textLabel.snp_bottom).offset(10)
            
            make.width.equalTo(100)
            
            make.height.equalTo(30)
            
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            
            make.right.equalTo(textLabel.snp_right)
            
            make.top.equalTo(textLabel.snp_bottom).offset(10)
            
            make.width.equalTo(100)
            
            make.height.equalTo(30)
        }
        
        coverImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self.snp_center)
        }
        
    
    }
    
    /// 设置动画
    func startRotationAnimation(){
    
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_2_PI
        
        animation.repeatCount = MAXFLOAT
        
        animation.duration = 5
        
        animation.removedOnCompletion = false
        
        rotationImageView.layer.addAnimation(animation, forKey: nil)
    
    
    }
    //私有懒加载
    //旋转图片
    private lazy var rotationImageView : UIImageView = {
    
        let rotationImageView = UIImageView()
        
        rotationImageView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return rotationImageView
        
    
    }()
    //中间图片
    private lazy var centerImageView : UIImageView = {
    
        let centerImageView = UIImageView()
        
        centerImageView.image = UIImage(named: "visitordiscover_feed_image_house")
        
        
        return centerImageView
    
    
    }()
    //文字
    private lazy var textLabel : UILabel = {
    
        let textLabel = UILabel()
        
        textLabel.text = "关注一些人，回这里看看有什么惊喜"
        
        textLabel.textAlignment = .Center
        
        textLabel.numberOfLines = 0
        
        textLabel.textColor = UIColor.darkGrayColor()
        
        textLabel.font = UIFont.systemFontOfSize(14)
        
        return textLabel
    
    
    }()
    //注册按钮
    private lazy var registerButton : UIButton = {
        
        let registerButton = UIButton()
        
        registerButton.setTitle("注册", forState: UIControlState.Normal)
        
        registerButton.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        registerButton.titleLabel?.font = UIFont.systemFontOfSize(16)
        
        registerButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        registerButton.addTarget(self, action: "registerButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return registerButton
    
    }()
    //登录按钮
    private lazy var loginButton : UIButton = {
    
        let loginButton = UIButton()
        
        loginButton.setTitle("登录", forState: UIControlState.Normal)
        
        loginButton.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        loginButton.titleLabel?.font = UIFont.systemFontOfSize(16)
        
        loginButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        loginButton.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return loginButton
    
    }()
    //遮挡图片
    
    private lazy var coverImageView : UIImageView = {
    
        let coverImageView = UIImageView()
        
        coverImageView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return coverImageView
    
    }()
    
    func registerButtonClick(){
    
        delegate?.register()
    }
    
    func loginButtonClick(){
        
        delegate?.login()
    
    }
    
    
    

}
